// 导入express模块
var express = require('express');
// 导入路由模块
var router = express.Router();
// 导入url模块
var url = require('url');
// 导入mysql模块 
var mysql = require('mysql');
// 导入数据库配置信息
var dbconfig = require('../db/DBConfig');
// 导入SQL查询语句
var cstu = require('../sql/Cstusql');
// 导入自定义公共类
var util = require('../utils/util');
// 使用DBConfig.js的配置信息创建一个MySQL连接池
var pool = mysql.createPool(dbconfig.mysql);

/* POST menu listing. */
/* 根据班级获取学生接口 */
router.post('/get', function(req, res, next) {
    let params = {
        cid: req.body.cid,
    };
    // 启用连接池查询
    pool.getConnection(function(err, connection) {
        connection.query(cstu.queryCuData(params), function(err, results) {
            // 查询结果
            if (!util.isEmpty(results)) {
                res.send({ "success": true, "data": {results}, "msg": "学生获取成功", "code":200 });
                connection.release();
            } else {
                res.send({ "success": false, "data": {}, "msg": "学生获取失败", "code":400 });
            }
        });
    })
});
/* 添加学生 */
router.post('/add', function(req, res, next) {
    // 获取请求字段
    let params = {
        bid: req.body.cid,
        uid: req.body.mid
    };
    // 启用连接池查询
    pool.getConnection(function(err, connection) {
        connection.query(cstu.insertData(params), function(err, results) {
            if (!results==null) {
                res.send({ "success": true, "data": {}, "msg": "添加成功" });
                connection.release();
            } else {
                res.send({ "success": false, "data": {}, "msg": "添加失败" });
            }
        });
    });
});
/* 删除学生 */
router.post('/del', function(req, res, next) {
    // 获取请求字段
    let params = {
        id: req.body.id
    };
    // 启用连接池查询
    pool.getConnection(function(err, connection) {
        connection.query(cstu.deleteData(params), function(err, results) {
            if (util.isEmpty(results)) {
                if (!util.isEmpty(results)) {
                    res.send({ "success": true, "data": {}, "msg": "删除成功" });y
                } else {
                    res.send({ "success": false, "data": {}, "msg": "参数输入不合法" });
                }
                connection.release();
            } else {
                // res.send({ "success": false, "data": {}, "msg": "删除失败" });
            }
        });
    });
});


module.exports = router;